package com.azhi.common.result;

import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class Result {
    private Integer code;
    private String message;
    private Map<String,Object> data = new HashMap<>();

    private Result(){}//防止外部调用构造函数


    /**
     * 返回成功结果
     * @return
     */
    public static Result ok(){
        Result result = new Result();
        result.setCode(ResponseEnum.SUCCESS.getCode());
        result.setMessage(ResponseEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 返回失败结果
     * @return
     */
    public static Result error(){
        Result result = new Result();
        result.setCode(ResponseEnum.ERROR.getCode());
        result.setMessage(ResponseEnum.ERROR.getMessage());
        return result;
    }

    /**
     * 返回特定的结果
     * @return
     */
    public static Result setResult(ResponseEnum responseEnum){
        Result result = new Result();
        result.setCode(responseEnum.getCode());
        result.setMessage(responseEnum.getMessage());
        return result;
    }

    /**
     * 为data赋值
     * @param key
     * @param obj
     * @return
     */
    public Result data(String key, Object obj) {
        this.data.put(key,obj);
        return this;
    }

    public Result data(Map<String,Object> map) {
        this.setData(map);
        return this;
    }

    public Result message(String msg) {
        this.setMessage(msg);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }
}
